package com.example.game.hff;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/*****************************************************
 * Rank Database에 대한 DB 연동 기능을 구현하는 클래스
 * 제작일 : 2013.10.02
 * 제작자 : LJI 
 *****************************************************/

public class RankDao2 {

	public List<Rank> getRankDao() {

		return null;
	}
	
	public void temp () {
		List<Rank> rankList= getRankByScore();
		
		for (Rank rank : rankList) {
			System.out.println(rank.toString());
		}
	}

	
	public List<Rank> getRankByScore() {

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql ;
		
		ArrayList<Rank> ranks = new ArrayList<Rank>();
		
		try {
			String url = "jdbc:apache:commons:dbcp:/pool";
			conn = DriverManager.getConnection(url);
			
				sql = "SELECT name, score FROM rank "
						+ "ORDER BY score DESC, name DESC LIMIT 0, 10;";
			
			pstmt = conn.prepareStatement(sql.toString());
			rs = pstmt.executeQuery();

			while (rs.next()) {
				Rank rank = new Rank();				
				rank.setName(rs.getString(1));
				rank.setScore(rs.getInt(2));				
				ranks.add(rank);//위에서 생성된 객체를 목록에 추가
			}
		} catch (SQLException ex){
			System.out.println("RankDao:SQL Error");
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (rs != null) try { rs.close(); } catch (Exception ex) {}
			if (pstmt != null) try { pstmt.close(); } catch (Exception ex) {}
			if (conn != null) try { conn.close(); } catch (Exception ex) {}
		}		
		//조회된 데이터를 요청한 곳으로 반환
		return ranks;
		
		
		
		
		
		
		
	}
	public void insertRank(Rank rank) {
		//데이터베이스에 연락처 삽입
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try {			
			String url = "jdbc:apache:commons:dbcp:/pool";
			conn = DriverManager.getConnection(url);
			
			pstmt = conn.prepareStatement(
				"INSERT INTO rank(name, score) " + 
				"VALUES (? , ?);");
			pstmt.setString(1, rank.getName());
			pstmt.setInt(2, rank.getScore());			
			pstmt.executeUpdate();
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			if (rs != null) try { rs.close(); } catch (Exception ex){}
			if (pstmt != null) try { pstmt.close(); } catch (Exception ex){}
			if (conn != null) try { conn.close(); } catch (Exception ex){}
		}
		
	}
	
//	public void deleteRank(int no) {
//		
//		//데이터베이스에서 no에 해당하는 연락처 삭제
//		Connection conn = null;
//		PreparedStatement pstmt = null;
//		ResultSet rs = null;
//		
//		try {
//			String url="jdbc:apache:commons:dbcp:/pool";			
//			conn=DriverManager.getConnection(url);
//			
//			pstmt = conn.prepareStatement(
//				"DELETE FROM rank WHERE c_no = ?");
//			pstmt.setInt(1, no);
//			
//			pstmt.executeUpdate();
//		} catch (Exception ex) {
//			
//		} finally {
//			if (rs != null) try { rs.close(); } catch (Exception ex){}
//			if (pstmt != null) try { pstmt.close(); } catch (Exception ex){}
//			if (conn != null) try { conn.close(); } catch (Exception ex){}
//		}
//		
//	}
	
//	public void updateRank(Rank rank) {
//		
//		//데이터베이스에서 rank에 저장된 번호에 해당하는 연락처를
//		//rank에 저장된 이름, 점수로 변경
//		Connection conn = null;
//		PreparedStatement pstmt = null;
//		ResultSet rs = null;
//		
//		try {
//			String url="jdbc:apache:commons:dbcp:/pool";			
//			conn=DriverManager.getConnection(url);
//			
//			pstmt = conn.prepareStatement(
//				"UPDATE rank " + 
//				"SET c_name = ?, c_score = ? " + 
//				"WHERE c_no = ?");
//			pstmt.setString(1, rank.getName());
//			pstmt.setInt(2, rank.getScore());		
//			pstmt.setInt(3, rank.getNo());
//			
//			pstmt.executeUpdate();
//			
//		} catch (Exception ex) {
//			
//		} finally {
//			if (rs != null) try { rs.close(); } catch (Exception ex){}
//			if (pstmt != null) try { pstmt.close(); } catch (Exception ex){}
//			if (conn != null) try { conn.close(); } catch (Exception ex){}
//		}
//		
//	}
	
}
